package com.shanjupay.common.oss;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.shanjupay.common.domain.BusinessException;
import com.shanjupay.common.domain.CommonErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * 七牛云客户端工具类
 * </p>
 *
 * @author <a href=mailto:zhaoboying@itcast.cn> byZhao </a>
 * @version 1.0.0
 * @motto Talk is cheap, show me the code!
 * @since 2022-05-02 17:50:29
 */

public class QiNiuCloudClient {

    public static final Logger log = LoggerFactory.getLogger(QiNiuCloudClient.class);

    private String accessKey;
    private String secretKey;
    private String bucket;
    private String url;

    /**
     *  无参构造器
     */
    public QiNiuCloudClient() {
    }

    /**
     * 构造器
     * @param accessKey 访问密钥
     * @param secretKey 安全密钥
     * @param bucket    空间名称
     * @param url       url 文件下载时需要使用
     */
    public QiNiuCloudClient(String accessKey, String secretKey, String bucket, String url) {
        this.accessKey = accessKey;
        this.secretKey = secretKey;
        this.bucket = bucket;
        this.url = url;
    }

    /**
     * 上传文件到七牛云
     * @param file 文件字节数组
     * @param key  文件名
     */
    public void upload2cloud(byte[] file, String key) {
        try {
            Configuration cfg = new Configuration(Region.autoRegion());
            UploadManager uploadManager = new UploadManager(cfg);
            Auth auth = Auth.create(accessKey, secretKey);
            String token = auth.uploadToken(bucket);
            try {
                Response response = uploadManager.put(file, key, token);
                DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
                System.out.println(putRet.key);
                System.out.println(putRet.hash);
            } catch (QiniuException ex1) {
                Response response = ex1.response;
                System.err.println(response.toString());
                log.error("上传文件到七牛云:{}", ex1.getMessage());
                try {
                    log.error(response.bodyString());
                } catch (QiniuException ex2) {
                    // ignore
                }
                throw new BusinessException(CommonErrorCode.E_500001);
            }
        } catch (Exception e) {
            log.error("上传文件到七牛云:{}", e.getMessage());
            throw new BusinessException(CommonErrorCode.E_500002);
        }
    }


    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getBucket() {
        return bucket;
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
